import { ref } from 'vue';
import { getFormColType } from '@/api/web/db/dataquery/sdkDqDsTb';

export function useFieldOptions() {
  const fieldOptions = ref([]);
  const fieldJavaOptions = ref([]);

  getFormColType().then(({ data }) => {
    fieldOptions.value = data.reduce((pre, item) => {
      const { jdbcType } = item;
      if (jdbcType === 'VARCHAR') {
        const fItem = pre.find((item) => item.jdbcType === jdbcType);
        if (!fItem) {
          pre.push({ ...item, label: '文本' });
        }
      } else if (jdbcType === 'TIMESTAMP') {
        const fItem = pre.find((item) => item.jdbcType === jdbcType);
        if (!fItem) {
          pre.push({ ...item, label: '日期' });
        }
      } else {
        pre.push({ ...item });
      }
      return pre;
    }, []);
    fieldJavaOptions.value = data;
  });

  function formatFieldType(row, { property }) {
    const v = row[property];
    return fieldOptions.value.filter((k) => k.jdbcType === v)?.[0]?.label ?? '';
  }
  function formatFieldJavaType(row, { property }) {
    const v = row[property];
    return fieldOptions.value.filter((k) => k.value === v)?.[0]?.label ?? '';
  }
  return {
    fieldOptions,
    fieldJavaOptions,
    formatFieldType,
    formatFieldJavaType,
  };
}
